package com.zhangqi.zhxa.base.impl;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.zhangqi.zhxa.MainActivity;
import com.zhangqi.zhxa.base.BaseMenuDetailPager;
import com.zhangqi.zhxa.base.BasePager;
import com.zhangqi.zhxa.base.impl.menudetail.InteractMenuDetailPager;
import com.zhangqi.zhxa.base.impl.menudetail.NewsMenuDetailPager;
import com.zhangqi.zhxa.base.impl.menudetail.PhotosMenuDetailPager;
import com.zhangqi.zhxa.base.impl.menudetail.TopicMenuDetailPager;
import com.zhangqi.zhxa.domain.NewsMenuData;
import com.zhangqi.zhxa.global.Constants;
import com.zhangqi.zhxa.utils.CacheUtils;

import java.util.ArrayList;

/**
 * 新闻中心页面
 * @author  Zhang Qi
 * @date 2016-4-28
 */
public class NewsCenterPager extends BasePager {

    //菜单详情页集合
    private ArrayList<BaseMenuDetailPager> mMenuDetailPagers;
    private NewsMenuData mNewsMenuData;// 新闻分类信息网络数据

    public NewsCenterPager(Activity activity) {
        super(activity);
    }

    @Override
    public void initData() {
        //tvTitle.setText("新闻");

        /*TextView view = new TextView(mActivity);
        view.setText("新闻中心");
        view.setTextColor(Color.RED);
        view.setTextSize(22);
        view.setGravity(Gravity.CENTER);

        flContent.addView(view);*/

        //1.首先看看本地有没缓存
        // 2.有缓存，直接加载缓存
        String cache = CacheUtils.getCache(Constants.CATEGRORIES_URL, mActivity);
        if(!TextUtils.isEmpty(cache)){
            //有缓存，为了提升用户体验，没网络的时候还有看的，所以设置缓存
            Log.i("cc","---------------------------------发现缓存");
            processResult(cache);
        }

        //即使发现有缓存，仍继续调用网络，获取保证最新数据
        getDataFromServer();

    }

    /**
     * 从服务器获取数据
     * 需要权限：INTERNET
     */
    private void getDataFromServer() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET, Constants.CATEGRORIES_URL,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        //请求成功
                        String result = responseInfo.result;//获取json字符串
                        //Log.i("aa","result--------------------"+result);
                        //解析数据
                        processResult(result);

                        //写缓存
                        CacheUtils.setCache(Constants.CATEGRORIES_URL,result,mActivity);
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        //请求失败
                        e.printStackTrace();
                        Toast.makeText(mActivity, s, Toast.LENGTH_SHORT).show();
                    }
                });
    }

    /**
     * 解析json数据，用gson去解析
     * @param result
     */
    protected void processResult(String result){
        //gson-->json
        Gson gson = new Gson();
        mNewsMenuData = gson.fromJson(result, NewsMenuData.class);
        //Log.i("bb","解析结果：-----------------"+mNewsMenuData);

        //获取侧边栏对象
        MainActivity mainUI = (MainActivity) mActivity;
        LeftMenuFragment leftMenuFragment = mainUI.getLeftMenuFragment();
        // 将网络数据设置给侧边栏
        leftMenuFragment.setData(mNewsMenuData.data);

        //初始化4个菜单详情页
        mMenuDetailPagers = new ArrayList<BaseMenuDetailPager>();
        mMenuDetailPagers.add(new NewsMenuDetailPager(mActivity,mNewsMenuData.data.get(0).children));
        mMenuDetailPagers.add(new TopicMenuDetailPager(mActivity));
        mMenuDetailPagers.add(new PhotosMenuDetailPager(mActivity));
        mMenuDetailPagers.add(new InteractMenuDetailPager(mActivity));

        //菜单详情页-新闻作为初始页面
        setCurrentMenuDetailPager(0);

    }

    //给新闻中心页面的Fragment填充布局
    protected void setCurrentMenuDetailPager(int position){
        BaseMenuDetailPager pager = mMenuDetailPagers.get(position);

        //移除之前所有的view对象,清屏
        flContent.removeAllViews();
        flContent.addView(pager.mRootView);
        pager.initData();//初始化数据

        //更改标题
        tvTitle.setText(mNewsMenuData.data.get(position).title);
    }

}
